package com.lys.emsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lys.emsystem.entity.Intent;
import com.lys.emsystem.service.IIntentService;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.IntentVo;
import com.lys.emsystem.vo.StuJobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liyansheng
 * @since 2022-04-26
 */
@RestController
@CrossOrigin
@RequestMapping("/intent")
public class IntentController {

    @Autowired
    private IIntentService iIntentService;

    @PostMapping
    public Result search(@RequestBody IntentVo intentVo) {
        System.out.println(intentVo);
        IPage<Intent> page = iIntentService.search(new Page(intentVo.getPageIndex(), intentVo.getPageSize()),
                intentVo);
        if (page.getSize() > 0) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("totalCount", page.getTotal());
            map.put("list", page.getRecords());
            return Result.buildResult(Result.Status.OK, map);
        }else {
            return Result.buildResult(Result.Status.OK, "无数据");
        }

    }

    @PostMapping("/add")
    public Result add(@RequestBody Intent intent, @RequestHeader("token") String token) {
        Integer userId = JWTUtils.getUserId(token);
        intent.setStudentId(userId);
        if (iIntentService.save(intent)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/update")
    public Result update(@RequestBody Intent intent) {
        if (iIntentService.updateById(intent)) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Intent intent) {
        if (iIntentService.removeById(intent.getId())) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    @PostMapping("/delList")
    public Result delList(@RequestBody IntentVo intentVo) {
        if (iIntentService.removeByIds(Arrays.asList(intentVo.getIds()))) {
            return Result.buildResult(Result.Status.OK);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }
    }

    /**
     * 获取某学生下的意向数据
     * @param token
     * @return
     */
    @GetMapping()
    public Result getById(@RequestHeader("token") String token) {
        Map<String, Object> objectMap = JWTUtils.checkToken(token);
        QueryWrapper<Intent> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", objectMap.get("userId"));
        List<Intent> intentList = iIntentService.list(wrapper);
        if (!intentList.isEmpty()) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("list", intentList);
            map.put("count", intentList.size());
            return Result.buildResult(Result.Status.OK, map);
        } else {
            return Result.buildResult(Result.Status.ERROR);
        }

    }

    @GetMapping("/class")
    public Result getClassIntents(@RequestHeader("token") String token) {
        Integer userId = JWTUtils.getUserId(token);
        List<StuJobVo> stuIntents = iIntentService.getStuIntents(userId);
        if (stuIntents == null) {
            return Result.buildResult(Result.Status.ERROR,"无数据！");
        }
        return Result.buildResult(Result.Status.OK, stuIntents);
    }
}
